package com.hgw.easyrocketmq.batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 批量消息
 *
 * @author LinHuiBa-YanAn
 * @date 2023/2/14 11:21
 */
public class BatchProducer {

    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer mqProducer = new DefaultMQProducer("group1");
        mqProducer.setNamesrvAddr("124.222.223.222:9876;47.96.232.192:9876");
        mqProducer.start();
        String topic = "BatchBigTest2";
        List<Message> messages = new ArrayList<>(10000);
        for (int i = 1; i <= 1000; i++) {
            messages.add(new Message(topic, "TagA", "OrderId00" + i, ("Hello World " + i).getBytes()));
        }
        MqListSplitter listSplitter = new MqListSplitter(messages);
        while (listSplitter.hasNext()) {
            mqProducer.send(listSplitter.next());
        }
        mqProducer.shutdown();
    }

}
